//
//  BNDCTtachmentView.m
//  OfficeDoc
//
//  Created by pie on 2022/4/3.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDCTtachmentView.h"
#import "BNDPushVCHandler.h"
#import "BNDOfficeDocDetailsWebVc.h"
#import "BNDOfficeDocKit.h"

@interface BNDCTtachmentView ()
@property (strong, nonatomic) UIImageView *iconImageView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *sizeLabel;
@end

@implementation BNDCTtachmentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColorWithHex(0xF6F7F9);
        self.layer.borderColor = UIColorWithHex(0xE1E4EB).CGColor;
        self.layer.borderWidth = 0.5;
        self.layer.cornerRadius = 6;
        self.layer.masksToBounds = YES;
        
        _iconImageView = [[UIImageView alloc] initWithImage:BNDImageNamed(@"bnd_file_pdf")];
        [self addSubview:_iconImageView];
        [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.centerY.mas_equalTo(self);
            make.size.mas_equalTo(CGSizeMake(46, 46));
        }];
        
        _titleLabel = ({
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.font = BNDPFHeavyFont(13);
            titleLabel.textColor = UIColorWithHex(0x353739);
            titleLabel.text = @"原型文档.pdf";
            titleLabel.numberOfLines = 1;
            titleLabel;
        });
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.iconImageView.mas_right).offset(0);
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.top.mas_equalTo(self.iconImageView.mas_top).offset(8);
        }];
        
        _sizeLabel = ({
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.font = BNDPFRegularFont(12);
            titleLabel.textColor = UIColorWithHex(0xB4B8BE);
            titleLabel.text = @"100KB";
            titleLabel.numberOfLines = 1;
            titleLabel;
        });
        [self addSubview:_sizeLabel];
        [_sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.iconImageView.mas_right).offset(0);
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(2);
        }];
        
        @weakify(self)
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
            @strongify(self)
            BND_dispatch_main_sync_safe(^{
    
                BNDOfficeDocDetailsWebVc *vc = [[BNDOfficeDocDetailsWebVc alloc] init];
                vc.info = self.fileInfo;
                vc.title = self.fileInfo[@"fileName"];
                NSString *url = [[BNDPreference sharedInstance] handleProduceImageUrl:self.fileInfo[@"filePath"]];
                [vc loadDataMethod:url withState:self.model.attachmentDownload];
                [[BNDPushVCHandler currentSelectedNavController] pushViewController:vc animated:YES];
            });
        }];
        [self addGestureRecognizer:tap];
    }
    
    return self;
}

- (void)setFileInfo:(NSDictionary *)fileInfo
{
    _fileInfo = fileInfo;
    _titleLabel.text = fileInfo[@"fileName"];
    NSString *size = fileInfo[@"fileSize"];
    _sizeLabel.text = [NSString stringWithFormat:@"%@KB",size];
    
    if(size.intValue ==0){
        size = @"0";
    }
    _sizeLabel.text = [NSString stringWithFormat:@"%@",[self convertFileSize:size.intValue
                                                         ]];
    _iconImageView.image = BNDImageNamed([BNDPreference handleFileImageName:fileInfo[@"fileType"]]);

}

- (NSString *) convertFileSize:(long long)size
{
    long kb = 1024;
    long mb = kb * 1024;
    long gb = mb * 1024;
    
    if (size >= gb) {
        return [NSString stringWithFormat:@"%.1f GB", (float) size / gb];
    } else if (size >= mb) {
        float f = (float) size / mb;
        if (f > 100) {
            return [NSString stringWithFormat:@"%.0f MB", f];
        }else{
            return [NSString stringWithFormat:@"%.1f MB", f];
        }
    } else if (size >= kb) {
        float f = (float) size / kb;
        if (f > 100) {
            return [NSString stringWithFormat:@"%.0f KB", f];
        }else{
            return [NSString stringWithFormat:@"%.1f KB", f];
        }
    } else
        return [NSString stringWithFormat:@"%lld B", size];
}

@end

